<?php

namespace app\user\logic\user;

use app\user\model\user\FacePic as FacePicModel;
use think\facade\{Config, Filesystem, Request};
use think\File;
use eduline\upload\Stock;

/**
 * 附件管理
 */
class FacePic extends FacePicModel
{

    /**
     * 添加附件
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     */
    public function saveFacePic(File $file, array $filedata = [])
    {
        $data   = [
            'savename'  => $file->getFilename(),
            'savepath'  => $this->getSavepath($file->getPath()),
            'filename'  => $file->getFilename(),
            'filehash'  => $file->hash(),
            'filemd5'   => $file->hash('md5'),
            'mimetype'  => strtolower($file->getMime()),
            'extension' => $file->getExtension(),
            'filesize'  => $file->getSize(),
            'from'      => Request::header('E-DEVICE-TYPE', 'public'),
        ];

        if ($this->save($data)) {
            return $this->find($this->getAttr('id'));
        }

        return false;
    }

    /**
     * 获取上传储存的位置: 检测并过滤上传到云端的文件
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/26
     *
     * @param $extension
     * @param $stock
     * @return string
     */
    protected function getUploadToStock($extension, $stock)
    {
        $exts = Config::get('filesystem.cloud_filter_extension', '');
        $exts = explode(',', $exts);
        if (in_array($extension, $exts)) {
            return 'local';
        }

        return $stock;
    }

    /**
     * 获取保存的目录
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     *
     * @param    [type]                         $filepath [description]
     * @return   [type]                                   [description]
     */
    protected function getSavepath($filepath)
    {
        return $this->getUploadDir() . '/' . str_replace($this->getUploadPath(), '', $filepath);
    }

    /**
     * 获取文件的上传保存磁盘根路径
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     *
     * @param    [type]                         $path [description]
     * @return   [type]                               [description]
     */
    public function getDiskPath(): string
    {
        return Filesystem::getDiskConfig($this->getDiskName(), 'root');
    }

    /**
     * 上传保存目录名称
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     *
     * @return   [type]                         [description]
     */
    public function getUploadDir()
    {
        return Stock::getStockConfig('local')['upload_dir'];
    }

    /**
     * 当前储存磁盘名称
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     *
     * @return   [type]                         [description]
     */
    public function getDiskName()
    {
        return Stock::getStockConfig('local')['bucket'];
    }

    /**
     * 获取上传保存路径
     *
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-13
     *
     * @param string $path [description]
     * @return   [type]                               [description]
     */
    public function getUploadPath($path = ''): string
    {
        return $this->getDiskPath() . '/' . $this->getUploadDir() . '/' . $path;
    }

    /**
     * 根据文件ID获取附件
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/30
     *
     * @param int $id
     * @return array|\think\Model
     */
    public function getAttachById(int $id)
    {
        return $this->where('id', $id)->findOrFail();
    }

    /**
     * 附件类型是否是音视频
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-16
     *
     * @param AttachModel $attach [description]
     * @return boolean             [description]
     */
    public function isAudioOrVideo(AttachModel $attach)
    {
        return Util::isAudio($attach['mimetype'], $attach['extension']) || Util::isVideo($attach['mimetype'], $attach['extension']);
    }

    /**
     * 获取文件名称
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/7/20
     *
     * @param int $attachId
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAttachTitle($attachId)
    {
        $attach = $this->where('id', $attachId)->find();
        if ($attach) {
            return $attach->getAttr('filename');
        }

        return '未知';
    }
}
